## import functions
from cs1graphics import *
from time import sleep

## background
window = Canvas()
window.setTitle('Snyder')
window.setBackgroundColor('skyBlue')
grass = Rectangle(200, 80, Point(100,160))
grass.setFillColor('green')
grass.setBorderColor('green')
grass.setDepth(100)
window.add(grass)
sun = Circle(20, Point(50,30))
sun.setFillColor('yellow')
window.add(sun)

## target layer
target = Layer()
back = Circle(30)
back.setFillColor('white')
back.setDepth(49)
target.add(back)
blue = Circle(20)
blue.setFillColor('blue')
blue.setDepth(48)
target.add(blue)
red = Circle(10)
red.setFillColor('red')
red.setDepth(47)
target.add(red)
legs = Path(Point(-25,45), Point(0,0), Point(25,45))
legs.setBorderWidth(2)
target.add(legs)
target.move(160,110)
target.setDepth(75)
window.add(target)

## Dialogue
dialogue = Text('Click the target to fire an arrow', 10)
dialogue.move(100,170)
window.add(dialogue)

## Miss Dialogue
miss = Text('You missed window will close', 9)
miss.move(100, 58)
miss2 = Text('on next click', 9)
miss2.move(100,72)

## start of arrow template
arrowTemp = Layer()
tip = Polygon(Point(0,0), Point(-8,5), Point(-5,0), Point(-8,-5))
tip.setFillColor('white')
arrowTemp.add(tip)
shaft = Path(Point(-30,0), Point(-5,0))
shaft.setBorderWidth(2)
shaft.setDepth(51)
arrowTemp.add(shaft)
fleching = Polygon(Point(-30,0), Point(-33,-3), Point(-40,-3),
                   Point(-36,0), Point(-38,3), Point(-36,3))
fleching.setFillColor('white')
arrowTemp.add(fleching)
arrowTemp.move(15,120)

## Start of real programming   
def split(target): ## removes "<" and ">" from the string of the mouse location
    target = str(target) ## then spits the remaining string on the comma
    target = target.strip('><')
    target = target.split(',')
    return target
    
def main(): ## Starts the main part of the program
    xInt = 0.0 ## sets the x value of the initial position of the arrow
    yInt = 120.0 ## sets the y value
    misses = 0 ## initial number of misses
    rate = 14 ## number of time the arrow will move
    while True:  ## event loop
        action = window.wait() ## wait on user event
        if misses >= 1: ## if the user has missed then it closes the window
            window.close() ## and breaks out of the event loop
            break
        location = split(action.getMouseLocation()) # calls the split function to create a list from mouse location
        x, y = location[0], location[1] ## sets the x and y variables from the location list
        arrow2 = arrowTemp.clone( ) ##clones the arrow template to fire at the target
        window.add(arrow2) ## adds the arrow to the scene
        x = float(x) ## converts the x value from an int to a float
        y = float(y) ## converts the y value from an int to a float
        for i in range(rate): ## moves the arrow in inctiments set
            xx = x - xInt ## finds the difference of the mouse location x value and the initial position of the arrow
            xx = xx/rate ## divids that number by ten for the movement values
            yy = y - yInt ## finds the differencr of the mouse location y value and the initial position of the arrow
            yy = yy/rate ## divids that number by ten for the movement values
            sleep(0.01) ## sleeps by 0.01 seconds
            arrow2.move(xx,yy) ## moves the arrow by found values
        check = ((x - 160.0)*(x - 160.0)) + ((y - 110.0)*(y -110.0)) ## next two lines finds if the mouse location was outside of the circle
        checkR = 30.0*30.0
        if check > checkR: ## if the arrow is out side of the target area does this
            misses += 1 ## incriments the misses value
            window.add(miss)
            window.add(miss2)
            for i in range(rate): ## moves the arrow to the ground
                yy = 190 - y ## finds how many pixels the arrow needs to fall
                yy = yy/rate ## finds how many per loop
                sleep(0.01) ## sleeps for 0.01 seconds
                arrow2.move(1.5,yy) ## moves the arrow 3 x cords and found y cords
                arrow2.rotate(90.0/rate) ## rotates the arrow by so that the arrow is strait up on the ground
    
main()




































